﻿using System;
using System.Linq;
using Core.Contract.DataContract;
using www.Models;

namespace Data.DAO
{
    public class RageComic_MemeDAO//:BaseDAO<RELAXEntities,RelaxMember>
    {
        #region value
        private readonly RelaxDataContext _context = new RelaxDataContext();
        #endregion
    
        #region Ham lay du Lieu
        public IQueryable<RelaxRagcomicMemeDataContract> GetRelaxRagcomicMemeDataContracts()
        {
            var query = from c in _context.MEME_RAGCOMICs
                        select new RelaxRagcomicMemeDataContract
                        {
                           Id=Convert.ToInt32(c.id),
                            Image = c.image,
                            Type = Convert.ToInt32(c.type??0),
                            Active = Convert.ToInt32(c.active)
                        };
            return query;
        }
        #endregion 
     
        #region Add
        public void Add(MEME_RAGCOMIC memeRagcomic)
        {
            _context.MEME_RAGCOMICs.InsertOnSubmit(memeRagcomic);
            _context.SubmitChanges();

        }
        #endregion
    
        #region Ham Update

        public void Update(MEME_RAGCOMIC memeRagcomic)
        {
            var query = _context.MEME_RAGCOMICs.Where(c => c.id == memeRagcomic.id);
            if (query.Any())
            {
                var firstOrdefault = query.FirstOrDefault();
                if (firstOrdefault != null)
                {

                    firstOrdefault.image = memeRagcomic.image;
                    firstOrdefault.type = memeRagcomic.type;
                    firstOrdefault.active = memeRagcomic.active;
                }
                _context.SubmitChanges();
            }
        }
        #endregion 

        #region Ham Delete

        public void Delete(long sId)
        {
            var query = _context.MEME_RAGCOMICs.Where(c => c.id == sId);
            if (query.Any())
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                _context.MEME_RAGCOMICs.DeleteOnSubmit(query.FirstOrDefault());
                _context.SubmitChanges();
            }
        }
        #endregion
    }
}
